var app = angular.module('ma-forcebuilder.controllers', ['ui.state', 'ma-forcebuilder.services']);

app.controller('FactionController', function($scope, $state, Faction) {

    $scope.factions = Faction.list();

    $scope.$watch('factions', function(newVal, oldVal){
        if (newVal) {
            $scope.faction = newVal[0];
            $scope.updatePath(newVal[0]);
        }
    });

    $scope.faction = {};


    $scope.updatePath = function(faction) {
        $state.transitionTo('list', { factionId: faction.name})
    }
});

app.controller('UnitsController', function ($scope, $state, $stateParams, Unit) {

    $scope.faction = $stateParams.factionId;
    $scope.units = Unit.allByFaction($stateParams.factionId);

    $scope.updatePath = function(unit) {
        var urlParams = { factionId: $scope.faction, unitId: unit.id};
        $state.transitionTo('details', urlParams);
    }
});

app.controller('UnitDetailController', function ($scope, $stateParams) {


});